package com.sfeir.gwt.webding.client.showcase;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.gwt.webding.client.component.ClosableTabPanel;
import com.sfeir.gwt.webding.client.component.Info;
import com.sfeir.gwt.webding.client.component.Ossature;
import com.sfeir.gwt.webding.client.component.menu.Item;
import com.sfeir.gwt.webding.client.component.menu.ItemGroup;
import com.sfeir.gwt.webding.client.component.menu.Menu;
import com.sfeir.gwt.webding.client.component.menu.event.MenuItemSelectEvent;
import com.sfeir.gwt.webding.client.component.menu.event.MenuItemSelectHandler;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ShowCase implements EntryPoint, MenuItemSelectHandler {

    private static final class GeneratorInfo {
    }
    
    private static final String ITEM_TYPE_URL = "url";
    private static final String ITEM_TYPE_TAB = "tab";
    private Ossature ossature;
    private Menu menu;
    private ClosableTabPanel body;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        GWT.create(GeneratorInfo.class);
        createOssature();

        Item itemComponent = new ItemGroup("Label Component");
        Item field = new ItemGroup("Champs");
        field.setOpen(true);
        field.add(new TabItem(new TabLabelTextBox()));
        field.add(new TabItem(new TabListBox()));
        field.add(new TabItem(new TabSuggestBox()));
        field.add(new TabItem(new TabLabelOther()));
        itemComponent.add(field);
        Item controls = new ItemGroup("Contrôles");
        controls.setOpen(true);
        controls.add(new TabItem(new TabFormator()));
        controls.add(new TabItem(new TabValidator()));
        itemComponent.add(controls);
        itemComponent.add(new TabItem(new TabForm()));
        itemComponent.add(new TabItem(new TabConfig()));
        menu.add(itemComponent);
        Item itemMenu = new ItemGroup("Menu");
        itemMenu.add(new TabItem(new TabFilterMenu()));
        itemMenu.add(new TabItem(new TabSearchMenu()));
        menu.add(itemMenu);
        Item itemLinks = new ItemGroup("Liens");
        itemLinks.add(new Item("Sfeir", "http://www.sfeir.com", ITEM_TYPE_URL));
        menu.add(itemLinks);

        body = ossature.getBody();
        body.addAndShow(createHomePage(), "Home");

        RootPanel.get().add(ossature);
    }

    public void onMenuItemSelect(MenuItemSelectEvent event) {
        Item item = event.getMenuItem();
        Info.display("Lancement de la page " + item.getLabel());
        if (item instanceof TabItem) {
            createTab(((TabItem) item).getWidget(), item.getLabel());
        } else if (item.getType().equals(ITEM_TYPE_URL)) {
            Window.open(item.getUrl(), "_blank", "");
        }
    }

    private void createTab(Widget widget, String label) {
        if (widget != null) {
            int widgetIndex = body.getWidgetIndex(widget);
            if (widgetIndex < 0)
                body.addAndShow(widget, label, false, true);
            else
                body.selectTab(widgetIndex);
        }
    }

    private Widget createHomePage() {
        VerticalPanel panel = new VerticalPanel();
        panel.add(new Image("logo.gif"));
        panel.add(new Label("Bienvenue sur le showcase des composants."));
        return panel;
    }

    /**
     * Initialise l'ossature Affiche le haut de page
     * @return
     */
    private void createOssature() {
        final RootPanel rootPanel = RootPanel.get();
        ossature = new Ossature();
        rootPanel.add(ossature);
        ossature.setStatus(DateTimeFormat.getMediumDateFormat().format(new Date()));
        AbsolutePanel header = ossature.getHeader();
        header.add(new Image("logo.gif"), 0, 0);
        createMenu(ossature);
    }

    private void createMenu(final Ossature ossature) {
        menu = new Menu();
        menu.setWidth("250px");
        menu.setHeight(Window.getClientHeight() - 108);
        Window.addResizeHandler(new ResizeHandler() {
            public void onResize(ResizeEvent event) {
                int height = Window.getClientHeight() - 108;
                menu.setHeight(height);
            }
        });
        menu.setFullTextSearch(true);
        menu.setSearchBarEnabled(false);
        menu.addMenuItemSelectHandler(this);
        // Ajouter a l'ossuture globale
        ossature.setMenu(menu);

        Widget searchbar = menu.getSearchBar();
        searchbar.setWidth("250px");
        AbsolutePanel header = ossature.getHeader();
        header.add(searchbar, 0, 58);
    }

    class TabItem extends Item {
        /**
         * 
         */
        private static final long serialVersionUID = -1466359207714286L;
        private LazyTab widget;

        public TabItem(LazyTab widget) {
            super(widget.getName(), widget.getName(), ITEM_TYPE_TAB);
            setWidget(widget);
        }

        public void setWidget(LazyTab widget) {
            this.widget = widget;
        }

        public Widget getWidget() {
            return widget;
        }
    }
}
